//
//  NSDictionary+Honda.m
//  Honda
//
//  Created by Wang.Xinlei on 4/9/13.
//  Copyright (c) 2013 HeninHoo. All rights reserved.
//

#import "NSObject+Honda.h"
#import "NSDictionary+Honda.h"

@implementation NSDictionary (Honda)

+ (BOOL)validDictionary:(NSDictionary *)dictionary {
    return ([self valid:dictionary] && 0 < dictionary.count);
}

+ (BOOL)invalidDictionary:(NSDictionary *)dictionary {
    return ([self invalid:dictionary] || 0 == dictionary.count);
}

+ (NSDictionary *)dictionaryWithDictionaryOrNil:(NSDictionary *)dictionaryOrNil {
    return [[self alloc] initWithDictionaryOrNil:dictionaryOrNil];
}

- (id)initWithDictionaryOrNil:(NSDictionary *)dictionaryOrNil {
    if ([self.class invalid:dictionaryOrNil]) {
        return [self initWithDictionary:dictionaryOrNil];
    }
    else {
        return [self init];
    }
}

- (id)objectForNilOrKey:(id)key {
    if ([NSObject invalid:key]) {
        return nil;
    }
    
    return [self objectForKey:key];
}

@end



@implementation NSMutableDictionary (Honda)

+ (NSMutableDictionary *)mutableDictionaryWithDictionaryOrNil:(NSDictionary *)dictionaryOrNil {
    return [[self alloc] initWithDictionaryOrNil:dictionaryOrNil];
}

- (void)setNilOrObject:(id)nilOrObject forKey:(id<NSCopying>)aKey {
    if ([NSObject invalid:nilOrObject]) {
        [self removeObjectForKey:aKey];
    }
    else {
        [self setObject:nilOrObject forKey:aKey];
    }
}

@end
